package com.test.www.utils.db;

import com.test.www.dao.UsersMapper;
import com.test.www.model.Users;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 数据块操作工具
 */
public class MybatisSessionUtil {
    static SqlSessionFactory sqlSessionFactory = null;
    static{
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        try {
            sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("delSQLMapConfig.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test(){
        SqlSession session = sqlSessionFactory.openSession();
        Users bUser = new Users();
        bUser.setuName("admin");
        bUser.setuPass("123456");
        bUser.setuCtime( new Date());
        session.getMapper(UsersMapper.class).insert( bUser );
        session.commit();

        List<Users> lists = session.getMapper(UsersMapper.class).selectUsers();
        session.commit();
        //查询集合
        System.out.println(lists.size()+"=len\n");
        System.out.println(lists.get(0).getuName()+"\n");
        session.close();
    }

}
